package com.ketangpai.admin.mapper;

import com.ketangpai.admin.entity.Course;
import com.ketangpai.admin.entity.dto.CourseDTO;
import com.ketangpai.admin.entity.param.CountParam;
import com.ketangpai.admin.entity.param.UserAddCourseParam;
import com.ketangpai.admin.entity.param.UserCourseParam;
import org.apache.ibatis.annotations.*;

@Mapper
public interface CourseMapper {
    /**
     * 根据课程id查询课程
     */
    @Select("select * from course where id = #{courseId}")
    CourseDTO selectByCourseId(Integer courseId);
    /**
     * 根据课程id查询成员数量
     */
    @Select("select student_number from course where id = #{courseId}")
    Integer selectCountByCourseId(Integer courseId);

    @Select("select * from course where  course_code = #{courseCode}")
    Course selectByCourseCode(UserAddCourseParam userAddCourseParam);

    /**
     * 搜索
     */
    @Select("SELECT * FROM course WHERE id = #{courseId} " +
            "AND (course_name LIKE CONCAT('%', #{searchMessage}, '%') " +
            "OR course_code LIKE CONCAT('%', #{searchMessage}, '%'))")
    CourseDTO searchClass(UserCourseParam param);

    @Insert("insert into course(course_name,course_code,user_id,clazz,create_time,mode,course_detail,course_location,semester,academic_year,background_image,course_hour,student_number) " +
            "values(#{courseName},#{courseCode},#{userId},#{clazz},now(),#{mode},#{courseDetail},#{courseLocation},#{semester},#{academicYear},#{backgroundImage},#{courseHour},#{stuNum})")
    Boolean createCourse(CourseDTO course);

    @Update("update course set course_name=#{courseName},user_id=#{userId},clazz=#{clazz},mode=#{mode},course_detail=#{courseDetail},course_location=#{courseLocation},semester=#{semester},academic_year=#{academicYear},background_image=#{backgroundImage},course_hour=#{courseHour},student_number=#{stuNum},is_over=#{isOver} where id=#{id}" )
    Boolean updateCourse(CourseDTO course);

    @Delete("delete from course where id=#{courseId}")
    boolean deleteById(Integer courseId);

    @Update("update course set student_number=#{count} where id =#{courseId}")
    void updateStuNum(CountParam countParam);
}
